package com.ylfstu;

/**
 * @author:Joy_Yang
 * @date:2022/9/26
 * @version:1.0
 */
public class Homework06 {
    public static void main(String[] args) {
        Person person1 = new Person("唐僧", new Horse());
        person1.common();
        person1.common();
        person1.passRiver();
        person1.common();
        person1.passFireHill();
    }
}

interface Vehicles{
    void work();
}
class Horse implements Vehicles {

    @Override
    public void work() {
        System.out.println("骑马...");
    }
}
class Boat implements Vehicles {

    @Override
    public void work() {
        System.out.println("坐船...");
    }
}
class Plane implements Vehicles {

    @Override
    public void work() {
        System.out.println("飞机...");
    }
}

class VehiclesFactory{
    private static Horse horse = new Horse();
    public VehiclesFactory(){}
    public static Horse getHorse() {
        return horse;
    }
    public static Boat getBoat() {
        return new Boat();
    }
    public static Plane getPlane() {
        return new Plane();
    }
}
class Person extends VehiclesFactory{
    private String name;
    private Vehicles vehicles;
    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }
    //把具体思想，封装成方法
    public void passRiver() {
        if (!(vehicles instanceof Boat)) {
            vehicles = VehiclesFactory.getBoat();
        }
        vehicles.work();
    }
    public void common() {
        if (!(vehicles instanceof Horse)) {
            vehicles = VehiclesFactory.getHorse();
        }
        vehicles.work();
    }
    public void passFireHill() {
        if (!(vehicles instanceof Plane)) {
            vehicles = VehiclesFactory.getPlane();
        }
        vehicles.work();
    }
}
